﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyLib;

namespace ProcessControl.Logic
{
    public class BugetLogic : ProcessControl.Interface.IBuget
    {
        public enum eBudgetType { Capex = 0, PR = 1, PO = 2 }
        
        /// <summary>
        /// 更新预算PR余额
        /// </summary>
        /// <param name="BudgetCode">预算编号</param>
        /// <param name="balance">余额</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool UpdateBalaceFromPR(string BudgetCode, double balance)
        {
            string Msg;
            return UpdateBudgetBalace(BudgetCode, eBudgetType.PR.ToString(), balance, out Msg);
        }

        /// <summary>
        /// 更新预算PR余额
        /// </summary>
        /// <param name="BudgetCode">预算编号</param>
        /// <param name="balance">余额</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool UpdateBalaceFromPR(string BudgetCode, double balance, out string Msg)
        {
            return UpdateBudgetBalace(BudgetCode, eBudgetType.PR.ToString(), balance, out Msg);
        }

        /// <summary>
        /// 更新预算PO余额
        /// </summary>
        /// <param name="BudgetCode">预算编号</param>
        /// <param name="balance">余额</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool UpdateBalaceFromPO(string BudgetCode, double balance, out string Msg)
        {
            return UpdateBudgetBalace(BudgetCode, eBudgetType.PO.ToString(), balance, out Msg);
        }

        /// <summary>
        /// 更新预算CAPEX余额
        /// </summary>
        /// <param name="BudgetCode">预算编号</param>
        /// <param name="balance">余额</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool UpdateBalaceFromCAPEX(string BudgetCode, double balance, out string Msg)
        {
            return UpdateBudgetBalace(BudgetCode, eBudgetType.Capex.ToString(), balance, out Msg);
        }        

        /// <summary>
        /// 更新预算余额
        /// </summary>
        /// <param name="BudgetCode">预算编号</param>
        /// <param name="BudgetType">预算类型</param>
        /// <param name="balance">余额</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool UpdateBudgetBalace(string BudgetCode, string BudgetType, double balance, out string Msg)
        {
            if (BudgetCode == "")
            {
                Msg = "更新预算余额失败：请提供有效的预算编号！";
                return false;
            }
            else
            {
                string Sql = string.Format("select * from PROC_BUDGETSET where BUDGETCODE='{0}'", BudgetCode);
                DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable(Sql);
                if (dt.Rows.Count == 0)
                {
                    Msg = "更新预算余额失败：查询不到预算编号为" + BudgetCode + "的记录！";
                    return false;
                }
                else if (dt.Rows.Count == 1)
                {
                    String strField;
                    if (BudgetType == eBudgetType.Capex.ToString())
                    {
                        strField = "CAPEXBALANCE";
                    }
                    else if (BudgetType == eBudgetType.PR.ToString())
                    {
                        strField = "PRBALANCE";
                    }
                    else
                    {
                        strField = "POBALANCE";
                    }
                    double PreBalance = Convert.ToDouble(dt.Rows[0][strField].ToString());
                    string ID = dt.Rows[0]["ID"].ToString();
                    string UpdateSql = string.Format("update PROC_BUDGETSET set {0}={1} where ID = {2}; ", strField, PreBalance+balance, ID);
                    DataAccess.Instance("BizDB").ExecuteDataTable(UpdateSql);
                    Msg = "更新预算余额成功！";
                    return true;
                }
                else
                {
                    Msg = "更新预算余额失败：查询到多条预算编号为" + BudgetCode + "的记录！";
                    return false;
                }
            }
        }

        /// <summary>
        /// 根据预算编号查询预算
        /// </summary>
        /// <param name="BudgetCode">预算编号</param>
        /// <param name="balance">余额</param>
        /// <returns>成功返回true,失败返回false</returns>
        public DataTable QueryBudgetByBudgetCode(string BudgetCode)
        {
            string Sql = string.Format("select * from PROC_BUDGETSET where BUDGETCODE='{0}'", BudgetCode);
            DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable(Sql);
            return dt;
        }
    }
}
